import java.util.*;
import java.io.*;
import org.apache.commons.lang3.StringUtils;

class Line {
		private String aLine;
		private List<String> lineItems;
		private int nItemsInLine;
		private String strDelim;

		Line(String deLim) {
			strDelim = deLim;
			nItemsInLine = 0;
			lineItems = new ArrayList<String>();
		}

		Line(String strLine, String deLim) {
			strDelim = deLim;
			nItemsInLine = 0;
			lineItems = new ArrayList<String>();
			this.Set(strLine);
		}

		public int Set(String strLine) {
			aLine = strLine;
			nItemsInLine = 0;

			StringTokenizer strTok = new StringTokenizer(aLine, strDelim);
			while (strTok.hasMoreElements()) {
				String item = new String(strTok.nextElement().toString()) ;
				lineItems.add(item);
				nItemsInLine++;
			}

			return nItemsInLine;
		}

		private void UpdateLine()
		{
			String lineStr = new String();
			for (int n = 0; n < nItemsInLine ; n++) {
				lineStr += lineItems.get(n);
				lineStr += strDelim;
			}
			aLine = lineStr;

		}


		public boolean SetAt(int nAtColumn, String str) {
			if (nAtColumn < 0) // || nAtColumn > nItemsInLine-1)
				return false;

			if (nAtColumn <= nItemsInLine-1)
				lineItems.set(nAtColumn, str);
			else
			{
				lineItems.add(new String(str));
				nItemsInLine++;
			}

			this.UpdateLine();

			return true;
		}

		public boolean AddItemsToLine(String str) {
			this.SetAt(nItemsInLine, str);
			return true;
		}

		public boolean RemoveItemInLine(int nIndxAt, int nItems) {
			if (nIndxAt < 0 || nIndxAt > nItemsInLine-1)
						return false;


			System.out.println("***** Removing at: " + nIndxAt + " " + this.toString()+ " " + nItemsInLine);
			/*for (int n = 0;  n < nItems ; n++)
				this.lineItems.remove(nIndxAt+n);
			nItemsInLine = lineItems.size();
			*/

			List<String> strNewLine = new ArrayList<String>();

			for (int n = 0 ; n < nItemsInLine ; n++) {
				if ( n >= nIndxAt && n < (nIndxAt+nItems))
					continue;
				String str = lineItems.get(n);
				strNewLine.add(str);
			}

			lineItems = strNewLine;
			nItemsInLine = lineItems.size();

			UpdateLine();
			System.out.println("***** " + this.toString()+ " " + nItemsInLine);
			return true;
		}

		public int ItemsInLine() {
			return nItemsInLine;
		}

		public String Get(int at) {
			if (nItemsInLine == 0 || at < 0 || at > nItemsInLine-1)
				return new String("");

			return lineItems.get(at);
		}

			// returns first match from the line...
		public int GetItemIndex(String str) {
			if (nItemsInLine == 0)
				return -1;

			 for (int n = 0 ; n < nItemsInLine ; n++) {
			 	String strItem = lineItems.get(n);
			 	if (StringUtils.equals(strItem, str))
			 		return n;
				}

				return -1;
		}

		public String toString() {

			if (nItemsInLine == 0)
				return null;

			return aLine;
		}
	}
